package UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Server {
    public DatagramSocket datagramSocket=null;

    public Server(int sort) throws SocketException {
        datagramSocket=new DatagramSocket(sort);
    }
    public void start() throws IOException {
        while(true){
            DatagramPacket datagramPacket1=new DatagramPacket(new byte[4096],4096);
            System.out.println("服务器开始运行");
            datagramSocket.receive(datagramPacket1);
            String request=new String(datagramPacket1.getData(),0,datagramPacket1.getLength());
            String response=process(request);
            DatagramPacket datagramPacket2=new DatagramPacket(response.getBytes(),0,response.getBytes().length,datagramPacket1.getSocketAddress());
            datagramSocket.send(datagramPacket2);
            System.out.println("服务器响应数据为"+response);
        }
    }

    private String process(String requset) {
        return requset;
    }

    public static void main(String[] args) throws IOException {
        Server server=new Server(9090);
        server.start();
    }
}